package com.xingli.jingdongshop.controller;

import com.google.protobuf.InvalidProtocolBufferException;

import com.xingli.jingdongshop.dao.GoodsCabinetDao;
import com.xingli.jingdongshop.dao.GoodsDao;
import com.xingli.jingdongshop.dao.ShopDao;
import com.xingli.jingdongshop.entity.Goods;
import com.xingli.jingdongshop.entity.GoodsCabinet;
import com.xingli.jingdongshop.entity.Shop;
import com.xingli.jingdongshop.proto.MsgJinDouShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/jd/goods")
public class GoodsCotroller{

    @Autowired
    private ShopDao shopDao;

    @Autowired
    private GoodsCabinetDao goodsCabinetDao;

    @Autowired
    private GoodsDao goodsDao;



    @GetMapping("/test/add")
    public Object testAdd(){
        String[] demondCounts = {"1000","5000","10000","30000","50000","100000"};
        String[] demondPrice = {"10","50","100","300","500","1000"};

        String[] goldCounts = {"1000","50000","100000","300000","500000","1000000"};
        String[] goldPrice = {"10","50","100","300","500","1000"};

        for (int i = 1; i < 7; i++) {
            Goods goods = new Goods("钻石x"+demondCounts[i-1],0,Float.parseFloat(demondPrice[i-1]),String.valueOf(i),
                    "https://ddqp-1256883864.cos.ap-chengdu.myqcloud.com/shop/diamond"+i+".png",-1,1000,0
                    ,2,2,-1,100+i);
            goodsDao.save(goods);
        }

        for (int i = 1; i < 7; i++) {
            Goods goods = new Goods("金币x"+goldCounts[i-1],0,Float.parseFloat(goldPrice[i-1]),String.valueOf(i),
                    "https://ddqp-1256883864.cos.ap-chengdu.myqcloud.com/shop/gold"+i+".png",-1,1000,0
                    ,2,2,-1,200+i);
            goodsDao.save(goods);
        }
        return null;
    }

    @PostMapping("/add")
    public Object addGoods(Goods goods){
        return goodsDao.save(goods);
    }

    @GetMapping("/list")
    public Object getGoodsList(Pageable pageable, @RequestParam(defaultValue = "3") int cabinetId){
        return goodsDao.findByCabinetId(cabinetId,pageable);
    }

    @PostMapping("/cabinet/add")
    public Object addGoodsCabinet(GoodsCabinet goodsCabinet){
        return goodsCabinetDao.save(goodsCabinet);
    }

    @GetMapping("/cabinet/list")
    public Object getGoodsCabinetList(Pageable pageable, @RequestParam(defaultValue = "1") int shopId){
        return goodsCabinetDao.getByShopId(shopId,pageable);
    }

    @PostMapping("/myshop/add")
    public Object addGoodsShop(Shop shop){
        return shopDao.save(shop);
    }

    @GetMapping("/myshop/list")
    public Object getGoodsShopList(Pageable pageable){
        return shopDao.findAll(pageable);
    }


    @GetMapping(value = "/shop")
    public Object getShop(int shopId) throws InvalidProtocolBufferException {
        MsgJinDouShop.JinDouShop.Builder protoJindouShop = MsgJinDouShop.JinDouShop.newBuilder();
        Shop shop = shopDao.findById(shopId).
                get();
        protoJindouShop.setIsOpen(shop.isOpen());
        protoJindouShop.setUpdateTime(1);
        if (shop.isOpen()){
            List<GoodsCabinet> goodsCabinets = goodsCabinetDao.getByShopId(shopId);
            for (int i = 0; i < goodsCabinets.size(); i++) {
                MsgJinDouShop.GoodsCabinet.Builder protoGoodsCabinet= MsgJinDouShop.GoodsCabinet.newBuilder();
                protoGoodsCabinet.setCabId(goodsCabinets.get(i).getId());
                protoGoodsCabinet.setCabName(goodsCabinets.get(i).getCabName());
                protoGoodsCabinet.setSortIdx(goodsCabinets.get(i).getSortIdx());
                List<Goods> goodsList = goodsDao.findByCabinetId(goodsCabinets.get(i).getId());
                for (int j = 0; j < goodsList.size(); j++) {
                    MsgJinDouShop.GoodsSlot.Builder protoGoods= MsgJinDouShop.GoodsSlot.newBuilder();
                    protoGoods.setGoodsId(goodsList.get(j).getId());
                    protoGoods.setGoodsName(goodsList.get(j).getGoodsName());
                    protoGoods.setIconId(goodsList.get(j).getIconId());
                    protoGoods.setStackCnt(goodsList.get(j).getPropsNumber());
                    protoGoods.setPrice((int) goodsList.get(j).getGoodsPrice());
                    protoGoods.setPersonalLimit(goodsList.get(j).getPersonalLimit());
                    protoGoods.setLimitResetPeriod(goodsList.get(j).getLimitResetPeriod());
                    protoGoods.setInventory(goodsList.get(j).getInventory());
                    protoGoods.setExpireTime(goodsList.get(j).getExpireTime());
                    protoGoods.setSortIdx(Integer.parseInt(goodsList.get(j).getGoodsSort()));
                    protoGoods.setItemId(goodsList.get(j).getPropsId());
                    protoGoods.setActionType(goodsList.get(j).getActionType());
                    protoGoodsCabinet.addGoodsList(j,protoGoods);
                }
                protoJindouShop.addCabList(i,protoGoodsCabinet);
            }
        }
//
//        String retnn = Base64.getEncoder().encodeToString(protoJindouShop.build().toByteArray());
//        System.out.println(retnn);
        return Base64.getEncoder().encodeToString(protoJindouShop.build().toByteArray());
    }
}
//CAEQARoSCAESDOenr+WIhuS4k+WMukACGhIIAhIM5Y2h57G75LiT5Yy6QAE=
//